//
//  CODeviceHelper.swift
//  CocoProject
//
//  Created by Vill on 2023/11/2.
//

import UIKit
import CoreTelephony

class CODeviceHelper: NSObject {
    
    /// 获取ISO代码
    class func netWorkIsoCodeProvide() -> String {
        let info = CTTelephonyNetworkInfo()
        var supplier:String = ""
        if #available(iOS 12.0, *) {
            if let carriers = info.serviceSubscriberCellularProviders {
                if carriers.keys.count == 0 {
                    return "NoSIM"
                } else { //获取运营商信息
                    if let carrierProviders = info.serviceSubscriberCellularProviders {
                        for item in carrierProviders.values {
                            if item.isoCountryCode != nil {
                                supplier = item.isoCountryCode ?? ""
                            }
                        }
                    }
                    if supplier == "," || supplier == "" {
                        supplier = "NoSIM"
                    }
                    return supplier
                }
            } else{
                return "NoSIM"
            }
        } else {
            if let carrier = info.subscriberCellularProvider {
                guard carrier.isoCountryCode != nil else { return "NoSIM" }
                return carrier.isoCountryCode!
            } else{
                return "NoSIM"
            }
        }
    }
    
    // 手机型号
    class func getMachineInfo() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        switch identifier {
            ///iPhone
            case "iPhone3,1", "iPhone3,2", "iPhone3,3":  return "iPhone 4"
            case "iPhone4,1":  return "iPhone 4s"
            case "iPhone5,1":  return "iPhone 5"
            case "iPhone5,2":  return "iPhone 5"
            case "iPhone5,3", "iPhone5,4":  return "iPhone 5c"
            case "iPhone6,1", "iPhone6,2":  return "iPhone 5s"
            case "iPhone7,2":  return "iPhone 6"
            case "iPhone7,1":  return "iPhone 6 Plus"
            case "iPhone8,1":  return "iPhone 6s"
            case "iPhone8,2":  return "iPhone 6s Plus"
            case "iPhone8,4":  return "iPhone SE"
            case "iPhone9,1", "iPhone9,3":  return "iPhone 7"
            case "iPhone9,2", "iPhone9,4":  return "iPhone 7 Plus"
            case "iPhone10,1", "iPhone10,4": return "iPhone 8"
            case "iPhone10,2", "iPhone10,5": return "iPhone 8 Plus"
            case "iPhone10,3", "iPhone10,6": return "iPhone X"
            case "iPhone11,2":  return "iPhone XS"
            case "iPhone11,4","iPhone11,6": return "iPhone XS MAX"
            case "iPhone11,8" : return "iPhone XR"
            case "iPhone12,1" : return "iPhone 11"
            case "iPhone12,3":  return "iPhone 11 Pro"
            case "iPhone12,5" : return "iPhone 11 Pro Max"
            case "iPhone12,8" : return "iPhone SE 2"
            case "iPhone13,1" : return "iPhone 12 mini"
            case "iPhone13,2" : return "iPhone 12"
            case "iPhone13,3" : return "iPhone 12 Pro"
            case "iPhone13,4" : return "iPhone 12 Pro Max"
            case "iPhone14,4" : return "iPhone 13 mini"
            case "iPhone14,5" : return "iiPhone 13"
            case "iPhone14,2" : return "iPhone 13 Pro"
            case "iPhone14,3" : return "iPhone 13 Pro Max"
            case "iPhone14,6" : return "iPhone SE (3rd generation)"
            case "iPhone14,7" : return "iPhone 14"
            case "iPhone14,8" : return "iPhone 14 Plus"
            case "iPhone15,2" : return "iPhone 14 Pro"
            case "iPhone15,3" : return "iPhone 14 Pro Max"
            default:  return identifier
        }
    }
    
    // 时区
    class func getlocaleCode() -> String {
        let locale = NSLocale.current.identifier
        return locale
    }
    
    // 时区
    class func getTimeZoneStr() -> String {
        NSTimeZone.resetSystemTimeZone()
        let timeZone:NSTimeZone = NSTimeZone.system as NSTimeZone
        return timeZone.name
    }
    
    // 时区
    class func getTimeZoneIsAsia() -> Bool {
        NSTimeZone.resetSystemTimeZone()
        let timeZone:NSTimeZone = NSTimeZone.system as NSTimeZone
        let timeZoneName = timeZone.name
        if (timeZoneName != "Asia/Chongqing" && timeZoneName != "Asia/Harbin" && timeZoneName != "Asia/Hong_Kong" &&
            timeZoneName != "Asia/Hong_Kong" && timeZoneName != "Asia/Shanghai" && timeZoneName != "Asia/Shanghai") {
            return false
        }
        return true
    }
    
    class func getOSCurrentFirstLanguage() -> Bool {
        let language = Locale.preferredLanguages.first ?? ""
        var firstCn = false
        if language.hasPrefix("zh") {
            // 判断语言是否为中文
            if let regionCode = Locale.current.regionCode, (regionCode == "CN" || regionCode == "TW" || regionCode == "HK") {
                // 判断地区是否为中国大陆、台湾或香港
                firstCn = true
            }
        }
        return firstCn
    }
    
    class func getOSCurrentLanguage() -> String {
        // 返回设备曾使用过的语言列表
        let languages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
        // 当前使用的语言排在第一
        let currentLanguage = languages.first
        return currentLanguage ?? "en-CN"
    }
    
    // 获取手机语言列表
    class func getOSLanguageList() -> [String] {
        // 返回设备曾使用过的语言列表
        let languages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
        return languages
    }
    
    // 获取手机语言列表是否包含中文
    class func getIsoLanguageContainZh() -> Bool {
        let languages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
        var laugageList = [String]()
        for i in 0..<languages.count {
            let str = languages[i]
            var countryCode = (NSLocale.current as NSLocale).object(forKey: .countryCode)
            if countryCode != nil{
                countryCode = str.replacingOccurrences(of: "-\(countryCode!)", with: "")
            }
            laugageList.append(countryCode as! String)
        }
        for i in 0..<laugageList.count {
            let str = laugageList[i]
            if (str == "zh-Hant-TW" || str == "zh-Hant-MO" || str == "zh-Hant-HK" || str == "zh-Hant" ||
                str == "zh-Hans-MO" || str == "zh-Hans-HK" || str == "zh-Hans-CN" || str == "zh-Hans" ||
                str == "zh" || str == "yue-Hans-CN" || str == "yue-Hant-CN"){
                return true
            }
        }
        return false
    }
    
    // 是否ipad
    public static func isMachineIpa() -> Bool {
        let device = UIDevice.current
        if device.userInterfaceIdiom == .phone {
            return false
        } else {
            return true
        }
    }
    
    // 是否包含中文
    public static func getKeyboardHasZn() -> Bool {
        let installedKeyboards = CODeviceHelper.getInstalledKeyboards()
        for i in 0..<installedKeyboards.count {
            let str = installedKeyboards[i]
            if str.contains("zh") || str.contains("yue"){
                return true
            }
        }
        return false
    }
    
    public static func getInstalledKeyboards() -> [String] {
        var keyboards = [String]()
        if let keyboardSettings = UserDefaults.standard.dictionaryRepresentation()["AppleKeyboards"] as? [String] {
            for key in keyboardSettings {
                if key != "Automatic" {
                    keyboards.append(key)
                }
            }
        }
        return keyboards
    }

}
